<!doctype html>
[#escape x as (x)!?html]
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>搜索 - ${site.seoTitle} - Powered by JPROCMS</title>
  <meta name="keywords" content="${site.seoKeyword}">
  <meta name="description" content="${site.seoDescription}">
  [#include '../includes/common_res.html'/]
  <style>
    #suggestions {
        border: 1px solid #ccc;
        display: none;
    }
    #suggestions div {
        padding: 5px;
        cursor: pointer;
    }
    #suggestions div:hover {
        background-color: #f0f0f0;
    }
</style>
</head>
<body>
[#assign shadowNav=true/]
[#include '../includes/header.html'/]
<div class="container">
  <div class="collapse d-xl-block mt-3">
    <form action="${base}/search">
      <div class="input-group">
        <input type="text" class="form-control"  id="search-input" name="q" value="${Params.q}" placeholder="搜索" aria-label="搜索">
        <div class="input-group-append">
          <button class="btn btn-outline-secondary" type="submit">
            <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-search" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
              <path fill-rule="evenodd" d="M10.442 10.442a1 1 0 0 1 1.415 0l3.85 3.85a1 1 0 0 1-1.414 1.415l-3.85-3.85a1 1 0 0 1 0-1.415z"></path>
              <path fill-rule="evenodd" d="M6.5 12a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zM13 6.5a6.5 6.5 0 1 1-13 0 6.5 6.5 0 0 1 13 0z"></path>
            </svg>
          </button>
        </div>
      </div>
      <div id="suggestions"></div>
    </form>
  </div>
  [#if Params.q!?trim != '']
  [@ZContentSearchPage  q=Params.q!  qMathPos=Params.qMathPos! pageSize=20 ; pageResult]
    <h3 class="mt-3">搜索结果</h3>
    <p class="small text-muted">为您找到相关结果 ${pageResult.total} 篇</p>
    <ul class="list-unstyled">
      [#list pageResult.list as content]
        <li class="mt-3 media border-bottom position-relative pb-3">
          [#if content.sysImage??][@ZUrl object=content class="mr-3"]<img src="${content.sysImage}" class="rounded-sm common-media-img" alt="${content.title}">[/@ZUrl][/#if]
          <div class="media-body es-highlight">
            <p class="mt-0 mb-1">[@ZUrl object=content class="common-link text-decoration-none"][#noescape]${content.highlightTitle!content.title!}[/#noescape][/@ZUrl]</p>
            <p class="text-muted d-none d-sm-block mb-2">
              [@ZUrl object=content class="text-reset small text-decoration-none"][#noescape]
                 [#if content.highlightTxt??]${ZSubstring(content.highlightTxt,100,'...')!}[#else]${ZSubstring(content.plainText,100,'...')!}[/#if]
              [/#noescape][/@ZUrl]
            </p>
            <div class="small text-muted mt-3 clearfix">
              <div class="float-left">
                <span>[#list content.channel.nodeList as c][@ZUrl object=c/][#if c_has_next] > [/#if][/#list]</span>
              </div>
              <div class="float-right"><i class="ml-2 far fa-clock"></i> ${ZFormat(content.releaseTime, 'yyyy-MM-dd')}</div>
            </div>
          </div>
        </li>
      [/#list]
    </ul>
    [#include '../includes/inc_page.html'/]
  [/@ZContentSearchPage ]
  [/#if]
</div>
[#include '../includes/footer.html'/]
</body>
<script>
   $(document).ready(function () {
    let suggestions = [];
    // 监听输入框的输入事件
    $('#search-input').on('input', function () {
      const inputValue = $(this).val().trim();
      if (inputValue === '') {
          $('#suggestions').hide();
          return;
      }
      //查询搜索词
      axios.get('${api}/sys-word/getSearchSuggest?text='+inputValue).then(function (response) {
        console.log(response.data)
        const datas = response.data.data;
        if(response.data.code==0&&datas.length > 0){
          suggestions = datas.map(w => w.word);
          // 筛选匹配的联想词
          const filteredSuggestions = suggestions.filter(suggestion =>
              suggestion.includes(inputValue)
          );
          if (filteredSuggestions.length === 0) {
              $('#suggestions').hide();
              return;
          }
          const suggestionDivs = filteredSuggestions.map(item => {
                return "<div>"+item+"</div>";
          });
          const suggestionHTML = suggestionDivs.join('');
          // 更新联想词列表并显示
          $('#suggestions').html(suggestionHTML).show();
        }else{
          $('#suggestions').hide();
          return;
        }
    });
  });
  // 点击联想词时，将其填充到输入框
  $('#suggestions').on('click', 'div', function () {
      const selectedSuggestion = $(this).text();
      $('#search-input').val(selectedSuggestion);
      $('#suggestions').hide();
  });
});
</script>
</html>
[/#escape]
